package server.controller;

import java.util.LinkedList;


import shared.messages.Message;
import server.util.Logger;

/**
 * The Class OutboundMessages
 * - used to store outgoing messages from the server.
 */
public class OutboundMessages {
	
	/** The messagelist for messages. */
	private LinkedList<Message> messages = new LinkedList<Message>();
	
	/** The logger for logging errors. */
	private Logger logger;
	
	/**
	 * Instantiates a new OutboundMessages.
	 *
	 * @param logger the logger
	 */
	public OutboundMessages(Logger logger) {
		this.logger = logger;
	}

	/**
	 * Adds the message to the messages List.
	 *
	 * @param msg the message to be added
	 */
	public synchronized void addMessage(Message msg) {
		messages.add(msg);
		this.notifyAll();
	}

	/**
	 * Gets the first message and removes it from the list.
	 *
	 * @return the message
	 */
	public synchronized Message getMessage() { 
		try {
			while (messages.isEmpty())
				this.wait(); 
		} catch (InterruptedException e) {
			logger.logError("OutboundMessages - getMessage() - wait interrupted");
		}

		Message msg = messages.getFirst();
		messages.removeFirst();
		return msg;
	}
}